import axios from 'axios'

// 创建axios实例
const instance = axios.create({
    baseURL: '/camera',
    timeout: 10000,
    headers: {
        'Content-Type': 'application/json'
    }
})



// 请求拦截器
instance.interceptors.request.use(
    config => {
        // 可以在这里添加认证信息
        return config
    },
    error => {
        return Promise.reject(error)
    }
)

// 响应拦截器
instance.interceptors.response.use(
    response => {
        return response.data
    },
    error => {
        // 统一错误处理
        if (error.code === 'ERR_NETWORK') {
            error.message = `无法连接到服务器: ${location.origin}/api\n请检查后端服务是否启动`
        }
        return Promise.reject(error)
    }
)

// 获取所有班级列表
export const getAllClasses = () => {
    return instance.get('/get-all-class')
}

// 获取班级所有学生（用于初始化未签到列表）
export const getAllStudents = (classId, liveName) => {
    return instance.get('/get-all-students', {
        params: { classId, liveName }
    })
}

// 获取已签到学生列表（实时）
export const getSignIn = (classId, liveName) => {
    return instance.get('/get-sign-in', {
        params: { classId, liveName }
    })
}

export const updateSignIn = (userId,signStatues,liveName) =>{
    return instance.get('/update-sign-in',{
        params:{
            userId,
            signStatues,
            liveName
        }
    })
}

export const monitorFace = (userId, imageFile) => {
    const formData = new FormData();
    formData.append('image', imageFile);
    formData.append('userId', userId);
    
    return instance.post('/monitor-faces', formData, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}